package com.zrwl.poscloud.core.restapi.admin.baseInfo;

import com.zrwl.poscloud.core.annotion.AuthorityVerify.AuthorityVerify;
import com.zrwl.poscloud.core.annotion.OperationLogger.OperationLogger;
import com.zrwl.poscloud.core.global.MessageConf;
import com.zrwl.poscloud.core.global.SysConf;
import com.zrwl.poscloud.base.entity.Company;
import com.zrwl.poscloud.commons.exception.ThrowableUtils;
import com.zrwl.poscloud.commons.utils.ResultUtil;
import com.zrwl.poscloud.commons.validator.group.Delete;
import com.zrwl.poscloud.commons.validator.group.GetList;
import com.zrwl.poscloud.commons.validator.group.Insert;
import com.zrwl.poscloud.commons.validator.group.Update;
import com.zrwl.poscloud.xo.service.AdminService;
import com.zrwl.poscloud.xo.service.CompanyDictDataService;
import com.zrwl.poscloud.xo.service.CompanyService;
import com.zrwl.poscloud.xo.vo.AdminVO;
import com.zrwl.poscloud.xo.vo.CompanyDictDataVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 商户字典数据 controller api类
 *
 * @author 刘思童 AutoGenerate
 * @date 2021-04-26
 */
@RestController
@Api(value = "商户字典数据相关接口", tags = {"商户字典数据相关接口"})
@RequestMapping("/companyDictData")
@Slf4j
public class CompanyDictDataRestApi {
    @Autowired
    private CompanyDictDataService companyDictDataService;
    @Autowired
    private AdminService adminService;
    @Autowired
    private CompanyService companyService;
    @AuthorityVerify
    @ApiOperation(value = "获取商户字典数据列表", notes = "获取商户字典数据列表", response = String.class)
    @PostMapping("/getList")
    public String getList(@Validated({GetList.class}) @RequestBody CompanyDictDataVO companyDictDataVO,BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        log.info("获取商户字典数据列表: {}", companyDictDataVO);
        AdminVO adminVO = adminService.getMe();
        if(!adminVO.isCompany()){
            ResultUtil.errorWithMessage("仅商户用户支持此功能");
        }
        Company company = companyService.getById(adminVO.getCompanyUid());
        companyDictDataVO.setCompanyUid(adminVO.getCompanyUid());
        companyDictDataVO.setModelDictTypeUid(company.getCompanyDictTypeUid());
        return ResultUtil.successWithData(companyDictDataService.getPageList(companyDictDataVO));
    }
    @AuthorityVerify
    @ApiOperation(value = "按公司字典类型列表获取列表", notes = "按公司字典类型列表获取列表", response = String.class)
    @PostMapping("/getListByCompanyDictTypeList")
    public String getListByCompanyDictTypeList(@RequestBody List<String> dictTypeList) {
        AdminVO adminVO = adminService.getMe();
        if(!adminVO.isCompany()){
            ResultUtil.errorWithMessage("仅商户用户支持此功能");
        }
        if(dictTypeList.size()<0){
            return ResultUtil.result(SysConf.ERROR, MessageConf.OPERATION_FAIL);
        }
        Company company = companyService.getById(adminVO.getCompanyUid());
        return ResultUtil.successWithData(companyDictDataService.getListByCompanyDictTypeList(dictTypeList,company.getCompanyDictTypeUid(),adminVO.getCompanyUid()));
    }

    @AuthorityVerify
    @ApiOperation(value = "获取商户字典数据列表", notes = "获取商户字典数据列表", response = String.class)
    @PostMapping("/getAllList")
    public String getAllList(@Validated({GetList.class}) @RequestBody CompanyDictDataVO companyDictDataVO, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        log.info("获取商户字典数据列表: {}", companyDictDataVO);
        return ResultUtil.successWithData(companyDictDataService.getAllList(companyDictDataVO));
    }

    @AuthorityVerify
    @OperationLogger(value = "新增商户字典数据")    @ApiOperation(value = "新增商户字典数据", notes = "新增商户字典数据", response = String.class)
    @PostMapping("/add")
    public String add(@Validated({Insert.class}) @RequestBody CompanyDictDataVO companyDictDataVO,BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        log.info("新增商户字典数据: {}", companyDictDataVO);
        AdminVO adminVO = adminService.getMe();
        if(!adminVO.isCompany()){
            ResultUtil.errorWithMessage("仅商户用户支持此功能");
        }
        Company company = companyService.getById(adminVO.getCompanyUid());
        companyDictDataVO.setCompanyUid(adminVO.getCompanyUid());
        companyDictDataVO.setModelDictTypeUid(company.getCompanyDictTypeUid());
        return companyDictDataService.add(companyDictDataVO);
    }

    @AuthorityVerify
    @OperationLogger(value = "修改商户字典数据")    @ApiOperation(value = "修改商户字典数据", notes = "修改商户字典数据", response = String.class)
    @PostMapping("/edit")
    public String edit(@Validated({Update.class}) @RequestBody CompanyDictDataVO companyDictDataVO, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        log.info("修改商户字典数据: {}", companyDictDataVO);
        AdminVO adminVO = adminService.getMe();
        if(!adminVO.isCompany()){
            ResultUtil.errorWithMessage("仅商户用户支持此功能");
        }
        Company company = companyService.getById(adminVO.getCompanyUid());
        companyDictDataVO.setModelDictTypeUid(company.getCompanyDictTypeUid());
        companyDictDataVO.setCompanyUid(adminVO.getCompanyUid());
        return companyDictDataService.edit(companyDictDataVO);
    }

    @AuthorityVerify
    @OperationLogger(value = "删除商户字典数据")    @ApiOperation(value = "删除商户字典数据", notes = "删除商户字典数据", response = String.class)
    @PostMapping("/delete")
    public String delete(@Validated({Delete.class}) @RequestBody CompanyDictDataVO companyDictDataVO, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        log.info("删除商户字典数据: {}", companyDictDataVO);
        return companyDictDataService.delete(companyDictDataVO);
    }

    @AuthorityVerify
    @ApiOperation(value = "获取商户字典类型列表", notes = "获取商户字典类型列表", response = String.class)
    @PostMapping("/getCompanyDictList")
    public String getCompanyDictList(@RequestBody CompanyDictDataVO companyDictDataVO) throws Exception {
        log.info("获取商户字典类型列表: {}", companyDictDataVO);
        return ResultUtil.successWithData(companyDictDataService.getCompanyDictList());
    }

    @AuthorityVerify
    @OperationLogger(value = "停用商户字典数据")    @ApiOperation(value = "停用商户字典数据", notes = "停用商户字典数据", response = String.class)
    @PostMapping("/deleteBatchCompanyDictData")
    public String deleteBatchCompanyDictData(@Validated({Delete.class}) @RequestBody List<CompanyDictDataVO> companyDictDataVOList, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        log.info("停用商户字典数据: {}", companyDictDataVOList);
        AdminVO adminVO = adminService.getMe();
        if(!adminVO.isCompany()){
            ResultUtil.errorWithMessage("仅商户用户支持此功能");
        }
        Company company = companyService.getById(adminVO.getCompanyUid());
        return companyDictDataService.deleteBatchCompanyDictData(companyDictDataVOList,company);
    }

//    @AuthorityVerify
//    @OperationLogger(value = "复制模版数据")    @ApiOperation(value = "复制模版数据", notes = "复制模版数据", response = String.class)
//    @PostMapping("/copyModelCompanyDictData")
//    public String copyModelCompanyDictData(@Validated({Insert.class}) @RequestBody CompanyDictDataVO companyDictDataVO, BindingResult result) {
//        ThrowableUtils.checkParamArgument(result);
//        log.info("复制模版数据: {}", companyDictDataVO);
//        return companyDictDataService.copyModelCompanyDictData(companyDictDataVO);
//    }

    @AuthorityVerify
    @OperationLogger(value = "获取公司字典数据集合体")    @ApiOperation(value = "获取公司字典数据集合体", notes = "获取公司字典数据集合体", response = String.class)
    @PostMapping("/getCompanyAllDictData")
    public String getCompanyAllDictData(@Validated({Insert.class}) @RequestBody CompanyDictDataVO companyDictDataVO, BindingResult result) throws Exception {
        ThrowableUtils.checkParamArgument(result);
        log.info("获取公司字典数据集合体: {}", companyDictDataVO);
        return ResultUtil.successWithData(companyDictDataService.getCompanyAllDictData(companyDictDataVO));
    }

}
